package com.fenghongzhang.day006;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * lv 适配器.
 *    数据源
 *    设置和数据源绑定.
 */
public class MainActivity extends AppCompatActivity {
    private ViewPager vp;

    private  List<ImageView> imageViewList;
    private static final String TAG = "MainActivity";
    private int index = 0;
    private LinearLayout ll;

    private TabLayout tab;

    private List<Fragment> list;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if(msg.what == 110){
                vp.setCurrentItem(index);
                index++;
                if(index > list.size()-1){
                    index = 0;
                }

            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll = (LinearLayout) findViewById(R.id.ll);

        tab = (TabLayout) findViewById(R.id.tab);

        vp = (ViewPager) findViewById(R.id.vp);

        //原生view 填充
//        List<VP> list = new ArrayList<>();
//
//        list.add(new VP("第一也","day006/src/main/res/mipmap-xxhdpi/ic_launcher.png"));
//        list.add(new VP("第二页","day006/src/main/res/mipmap-xxhdpi/ic_launcher.png"));
//
//        MyAdapter myAdapter = new MyAdapter(this, list);
//        vp.setAdapter(myAdapter);

        //fragment 来填充.
        list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwpFragment());
        list.add(new ThreeFragment());
        list.add(new ThreeFragment());
        list.add(new ThreeFragment());
        list.add(new ThreeFragment());
        list.add(new ThreeFragment());
        list.add(new ThreeFragment());
        list.add(new ThreeFragment());
        list.add(new ThreeFragment());


        //3.数据源,tablayout的数据源
        List<String> titlelist = new ArrayList<>();
        titlelist.add("新闻");
        titlelist.add("游戏");
        titlelist.add("音乐");
        titlelist.add("音乐");
        titlelist.add("音乐");
        titlelist.add("音乐");
        titlelist.add("音乐");
        titlelist.add("音乐");
        titlelist.add("音乐");
        titlelist.add("音乐");

        //4.把数据源传入到Fragment的适配器中.
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), list,titlelist);
        vp.setAdapter(fragmentAdapter);

        //7,需要把vp和tab绑定要一起.
        tab.setupWithViewPager(vp);




























        imageViewList = new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            ImageView imageView = new ImageView(this);
            if (i == 0){
                imageView.setImageResource(R.drawable.circle_t);
            }else {
                imageView.setImageResource(R.drawable.circle_f);
            }
            imageViewList.add(imageView);
            ll.addView(imageView);
        }

        //指定当前的页
//        vp.setCurrentItem();

        Timer timer = new Timer();
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                //子线程
//                //1,handler
////                vp.setCurrentItem(1);
////                handler.sendEmptyMessage(110);
//
//                //2 runOnUIThread()
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        //主线程中
//                        vp.setCurrentItem(index);
//                        index++;
//                        if(index > list.size()-1){
//                            index = 0;
//                        }
//                    }
//                });
//
//            }
//        },0,1000);

        //翻页动画.
//        vp.setPageTransformer(true,new DepthPageTransformer());


        //翻页监听
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /**
             *
             * @param position  当前页面
             * @param positionOffset
             * @param positionOffsetPixels
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                Log.i(TAG, "onPageScrolled: "+positionOffset);
//                Log.i(TAG, "onPageScrolled: "+positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
//                if(position ==1){
//                    Toast.makeText(MainActivity.this, "第二页", Toast.LENGTH_SHORT).show();
//                }else if(position == 2){
//                    Toast.makeText(MainActivity.this, "第三页", Toast.LENGTH_SHORT).show();
//                }

                for (int i = 0; i <imageViewList.size() ; i++) {
                    ImageView imageView = imageViewList.get(i);
                    if(i == position){
                        imageView.setImageResource(R.drawable.circle_t);
                    }else {
                        imageView.setImageResource(R.drawable.circle_f);
                    }

                }

            }


            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state){
                    case ViewPager.SCROLL_STATE_IDLE:
                    break;
                }
            }
        });

    }
}
